<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>公司管理</title>
    <% include("/common/common_css_js.html"){} %>
</head>
<body>
    <div style="padding-bottom: 5px;">
        <div id="queryForm" class="uc-toolbaroverflow" _borderStyle="border-top:0;">
            <span class="separator"></span>
            <span>公司名称：</span>
            <input id="keyName" name="keyName" class="mini-textbox" />
            <span>电话：</span>
            <input id="keyPhone" name="keyPhone" class="mini-textbox" />
            <span>注册地址：</span>
            <input id="keyRegisteredAddress" name="keyRegisteredAddress" class="mini-textbox" />
            <span>法人：</span>
            <input id="keyLegalPerson" name="keyLegalPerson" class="mini-textbox" />
            <div style="margin-top: 4px"></div>

            <% if(so.hasPermission("xtCompany:query")){%>
                <a class="mini-button" iconCls="icon-search" onclick="search()">查询</a>
            <% } %>

            <% if(so.hasPermission("xtCompany:queryCndClear")){%>
                <a class="mini-button" iconCls="icon-new" onclick="clearSearch()">清空查询条件</a>
            <% } %>

            <span class="separator"></span>

            <% if(so.hasPermission("xtCompany:add")){%>
                <a class="mini-button" iconCls="icon-add" onclick="addCompany()">新增</a>
            <% } %>

            <% if(so.hasPermission("xtCompany:edit")){%>
                <a class="mini-button" iconCls="icon-edit" onclick="editCompany()">修改</a>
            <% } %>

            <% if(so.hasPermission("xtCompany:delete")){%>
                <a class="mini-button" iconCls="icon-no" onclick="deleteCompany()">删除</a>
            <% } %>
        </div>
    </div>

    <div class = "mini-fit">
        <div id="datagrid1" class="mini-datagrid"
             style="width: 100%; height: 100%;"
             url="${base}/pages/basic_data_manage/xt_company/query" idField="id"
             showPager="true" pageSize="20" sizeList="[10,20,30,50,100]"
             allowResize="true"
             allowCellWrap="true">
            <div property="columns">
<!--                <div field="id" width="140" headerAlign="center" align="left" allowSort="true">ID</div>-->
                <div field="name" width="140" headerAlign="center" align="center" allowSort="true">公司名称</div>
                <div field="phone" width="140" headerAlign="center" align="center" allowSort="true">电话</div>
                <div field="registeredAddress" width="140" headerAlign="center" align="left" allowSort="true">注册地址</div>
                <div field="legalPerson" width="140" headerAlign="center" align="center" allowSort="true">法人</div>
                <div field="masterBillForeignCompanyId" width="140" headerAlign="center" align="center" data-options='{cacheName:"sysCompany"}' renderer="commonRender">主单对客户公司名</div>
            </div>
        </div>
    </div>

    <!-- 添加编辑区域 -->
    <div id="companyPanel" class="mini-window" title="公司管理" style="width:700px;height:500px;"
         showMaxButton="true" showCollapseButton="false" showMinButton="true" showShadow="true"
         showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true"
    >
        <div property="footer" style="padding:10px;text-align: right">
            <a  id="updateAndSaveBtn" name="updateAndSaveBtn" class="mini-button" iconCls="icon-save" onclick="updateAndSave()">保存</a>
            <a  id="addAndSaveBtn" name="addAndSaveBtn" class="mini-button" iconCls="icon-save" onclick="addAndSave()">保存</a>
        </div>

        <div id="companyForm">

            <table style="width: 100%">
                <tr>
                    <td><label>公司名称:</label></td>
                    <td>
                        <input id="id" name="id"  class="mini-hidden" readonly="readonly"/>
                        <input id="name" name="name"  class="mini-textbox" required="true" requiredErrorText="公司名称不能为空" style="width: 80%"/>
                    </td>
                </tr>

                <tr>
                    <td><label>电话:</label></td>
                    <td>
                        <input id="phone" name="phone"  class="mini-textbox" required="true" requiredErrorText="公司电话不能为空" style="width: 80%"/>
                    </td>
                </tr>

                <tr>
                    <td><label>注册地址:</label></td>
                    <td>
                        <input id="registeredAddress" name="registeredAddress"  class="mini-textbox" required="true" requiredErrorText="公司注册地址不能为空" style="width: 80%"/>
                    </td>
                </tr>

                <tr>
                    <td><label>法人代表:</label></td>
                    <td>
                        <input id="legalPerson" name="legalPerson"  class="mini-textbox" required="true" requiredErrorText="公司法人代表不能为空" style="width: 80%"/>
                    </td>
                </tr>

                <tr>
                    <td><label>对外公司名:</label></td>
                    <td>
                        <input id="masterBillForeignCompanyId" name="masterBillForeignCompanyId"  textField="name" valueField="id"  class="mini-combobox" required="false" requiredErrorText="对外公司名不能为空" style="width: 80%"/>
                    </td>
                </tr>

                <tr>
                    <td><label>经营范围:</label></td>
                    <td><input id="businessScope"  name="businessScope" style="width:80%;height:100px;text-align: left;" class="mini-textarea"   emptyText="填写公司经营范围"  /></td>
                </tr>
            </table>

        </div>
    </div>






    <script type="text/javascript">
        mini.parse();
        let grid = mini.get("datagrid1");

        let updateAndSaveBtn = mini.get("updateAndSaveBtn");
        let addAndSaveBtn = mini.get("addAndSaveBtn");

        let form = new mini.Form("#companyForm");

        $(function () {
            let masterBillForeignCompanyIdJsonData = commonRenderDropList("sysCompany");
            mini.get("masterBillForeignCompanyId").load(masterBillForeignCompanyIdJsonData);
            // 加载列表数据
            grid.load();
        });

        function search() {
            let form = new mini.Form("#queryForm");
            let data = form.getData(true);
            grid.load(data);
        }

        function clearSearch() {
            let form = new mini.Form("#queryForm");
            form.clear();
        }

        function addCompany() {
            form.clear();
            addAndSaveBtn.show();
            updateAndSaveBtn.hide();

            public_showModelAtPos("companyPanel", "center", "middle");
        }


        function addAndSave() {
            // 添加并保存
            form.validate();
            if (form.isValid() == false) return;
            let data = form.getData(true);

            let ret = public_getServerData("${base}/pages/basic_data_manage/xt_company/insert",data);
            if(ret.ok) {
                public_showTip(ret);
                // 重新刷新数据
                grid.reload();
                public_onCancel('companyPanel');
            }
        }
        
        function editCompany() {
            form.clear();
            // 获取当前选中的行
            let row = grid.getSelected();

            if(!row){
                mini.alert("请选择要编辑的行!","客户编辑",null);
                return;
            }

            form.setData(row);

            addAndSaveBtn.hide();
            updateAndSaveBtn.show();

            // 显示编辑面板
            public_showModelAtPos("companyPanel", "center", "middle");
        }

        function updateAndSave() {
            form.validate();
            if (form.isValid() == false) return;
            let data = form.getData(true);

            let ret = public_getServerData("${base}/pages/basic_data_manage/xt_company/update",data);
            if(ret.ok) {
                refreashCache();
                public_showTip(ret);
                // 重新刷新数据
                grid.reload();
                public_onCancel('companyPanel');
            }

        }

        function deleteCompany(){
            let select = grid.getSelected();
            if(select == null){
                public_showErrorTips("未选中");
                return;
            }
            mini.confirm(
                "确定删除？",
                "删除提示",
                function(action) {
                    if (action == "ok") {
                        let ret = public_getServerData("${base}/pages/basic_data_manage/xt_company/delete?id=" + select.id);
                        public_showTip(ret);
                        if(ret.ok) {
                            refreashCache();
                            // 重新刷新数据
                            grid.removeRow(select, true);
                        }
                    }
                });
        }
        // 刷新缓存，把刷新的缓存放到父页面，应当调用父级页面方法刷新
        function refreashCache() {
            parent.window.loadCache();
        }


    </script>
</body>
</html>